define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'bin/index' + location.search,
                    add_url: 'bin/add',
                    edit_url: 'bin/edit',
                    del_url: 'bin/del',
                    multi_url: 'bin/multi',
                    import_url: 'bin/import',
                    table: 'bin',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                searchFormVisible: true,
                fixedRightNumber: 2,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'bin_sn', title: __('箱号'), operate: 'LIKE'},
                        {field: 'type.name', title: __('Bin_type'), searchList: $.getJSON('bin.type/searchlist')},
                        {field: 'native.name', title: __('Bin_native'), searchList: $.getJSON('bin.native/searchlist')},
                        {field: 'current_status', title: __('Current_status'), searchList: {"1":__('Current_status 1'),"2":__('Current_status 2'),"3":__('Current_status 3'),"4":__('Current_status 4'),"5":__('Current_status 5')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('首次入场时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
/*
                        {field: 'firstentertime', title: __('首次入场时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
*/
                        {field: 'take_number', title: __('首次入场提单号'), operate: 'LIKE' },
                        {field: 'binSell.take_null_time', title: __('卖箱出场时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'binSell.take_number', title: __('卖箱出场提单号'), operate: 'LIKE' },
                        {field: 'yard.organ_name', title: __('当前堆场'), operate: 'LIKE'},
                        {field: 'updatetime', title: __('最后确认时间'), operate:false, addclass:'datetimerange', autocomplete:false},
                        {field: 'last_status', title: __('最后环节'), searchList: {"1":__('Last_status 1'),"2":__('Last_status 2'),"3":__('Last_status 3'),"4":__('Last_status 4'),"5":__('Last_status 5'),"6":__('Last_status 6'),"7":__('Last_status 7'),"8":__('Last_status 8')}, formatter: Table.api.formatter.status},
                        {field: 'bincompany.organ_name', title: __('箱源公司'), operate: 'LIKE'},
                        {field: 'ourcompany.organ_name', title: __('我方公司'), operate: 'LIKE'},
                        {field: 'order.purchase_type', title: __('Purchase_type'), searchList: {"1":__('Purchase_type 1'),"2":__('Purchase_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'origin_sn', title: __('系统箱号'), operate: 'LIKE'},
                        {field: 'state', title: __('State'), searchList: {"1":__('State 1'),"0":__('State 0')}, formatter: Table.api.formatter.status},
                        {field: 'remark', title: __('备注'),  align: 'left',operate: false},
                        {
                            field: 'last_rent_number',
                            title: __('最后租出单据号'),
                            operate: false,
                            formatter: function (value) {
                                if (value == '') {
                                    return '暂无';
                                }else{
                                    return value;
                                }
                            },
                        },

                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('租出频次'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: function(row,value){

                                        return '查看('+row.total_rent_time+')';


                                    },
                                    extend:'data-area=\'["1500px","90%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: '租出频次',
                                    classname: 'btn btn-xs btn-info btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: function(row,value){
                                        return "bin.rent/index/bin_id/"+row.id;
                                    },
                                    hidden:function (row){
                                      /*  if(row.current_status==2){
                                            return true;
                                        }*/
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('往来记录'),
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('历史'),
                                    extend:'data-area=\'["1500px","90%"]\' data-shade=\'[0.5,"#000"]\'',
                                    title: __('历史'),
                                    classname: 'btn btn-xs btn-success btn-addtabs',
                                    icon: 'fa fa-folder-o',
                                    url: function(row,value){
                                        return "bin.history/index/bin_id/"+row.id;
                                    },
                                    hidden:function (row){
                                        /*  if(row.current_status==2){
                                              return true;
                                          }*/
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.buttons
                        },


                        /*  {field: 'weigh', title: __('Weigh'), operate: false},
                          {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}*/
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
